import md5 from 'js-md5'
import useClipboard from 'vue-clipboard3'

const _K_ = 'b1ac42f5979c54ac468b09840f66ea9f'

// 获取用户信息
export const getUserinfo = () => {
	const local = uni.getStorageSync('user')
	return local ? JSON.parse(local) : {}
}

// 字符串处理
export const setStringStyle = (str = '') => {
	let num = str.length
	let left = str.substring(0, 4)
	let right = str.substring(num - 3)
	let res = left + '******' + right
	return num < 5 ? str : res
}

// 字符串处理
export const setStringStyleName = (str = '') => {
	if (str) {
		let arr = str.split('')
		return ' * * ' + arr[arr.length - 1]
	} else {
		return ''
	}
}

// 字符串处理
export const setStringStylecode = (str = '') => {
	let num = str.length
	let center = parseInt(num / 2)
	let left = str.substring(0, center - 5)
	let right = str.substring(center + 8)
	let res = left + '......' + right
	return num < 5 ? str : res
}

// 日期格式化
export const dateFormat = function(timestamp, formats) {
	// formats格式包括
	// 1. Y-m-d
	// 2. Y-m-d H:i:s
	// 3. Y年m月d日
	// 4. Y年m月d日 H时i分
	formats = formats || 'Y-m-d';

	var zero = function(value) {
		if (value < 10) {
			return '0' + value;
		}
		return value;
	};

	var myDate = timestamp ? new Date(timestamp) : new Date();

	var year = myDate.getFullYear();
	var month = zero(myDate.getMonth() + 1);
	var day = zero(myDate.getDate());

	var hour = zero(myDate.getHours());
	var minite = zero(myDate.getMinutes());
	var second = zero(myDate.getSeconds());

	return formats.replace(/Y|m|d|H|i|s/ig, function(matches) {
		return ({
			Y: year,
			m: month,
			d: day,
			H: hour,
			i: minite,
			s: second
		})[matches];
	});
}

// 复制
export const copyText = async (str) => {
	const {
		toClipboard
	} = useClipboard()
	try {
		await toClipboard(str)
		uni.showToast({
			title: '复制成功',
			icon: 'success'
		})
	} catch (e) {
		uni.showToast({
			title: '复制失败',
			icon: 'none'
		})
	}
}

export const isWeiXin = () => {
	let ua = window.navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == 'micromessenger') {
		return true
	} else {
		return false
	}
}

// 获取code参数
export const getUrlParam = (name) => {
	let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	let r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return unescape(r[2]);
	}
	return null;
}


// 加密
export const encryptStr = (str) => {
	return md5(str + '=' + _K_)
}

export const encryptPWD = (str) => {
	return md5(str)
}

// 排序
export const objKeySort = (obj) => { //排序的函数
	var newkey = Object.keys(obj).sort();
	//先用Object内置类的keys方法获取要排序对象的属性名，再利用Array原型上的sort方法对获取的属性名进行排序，newkey是一个数组
	var newObj = {}; //创建一个新的对象，用于存放排好序的键值对
	for (var i = 0; i < newkey.length; i++) { //遍历newkey数组
		newObj[newkey[i]] = obj[newkey[i]]; //向新创建的对象中按照排好的顺序依次增加键值对
	}
	return newObj; //返回排好序的新对象
}

// 打开popup
export const openPopup = (self, refName, location = 'buttom') => {
	self.$refs[refName].open(location)
}

// 关闭popup
export const closePopup = (self, refName) => {
	self.$refs[refName].close()
}

// 切换
export const popupToggle = (show, self, refName, location = 'buttom') => {
	show ? self.$refs[refName].open(location) : self.$refs[refName].close()
}



// navigateBack返回
export const navigateBack = (delta = '1', type = "pop-out", time = 100) => {
	uni.navigateBack({
		delta: 1,
		animationType: type,
		animationDuration: time
	})
}

// navigateTo跳转
export const navigateTo = (path, type = "pop-in", time = 100) => {
	uni.navigateTo({
		url: path,
		animationType: type,
		animationDuration: time
	})
}

// switchTab 跳转
export const switchTab = (path, type = "pop-in", time = 100) => {
	uni.switchTab({
		url: path,
		animationType: type,
		animationDuration: time
	})
}

// 前端错误
export const showErr = (err) => {
	console.log(err)
	uni.showToast({
		title: '服务繁忙',
		icon: 'error'
	})
}


export const onConfirmclick = (fn, data) => {
	uni.showModal({
		title: '购买须知',
		content: 'SKY艺术平台发行数字藏品为虚拟物品,仅限实名认证且年满十八周岁的用户购买。平台发行的数字藏品版权归发行方或创作者所有,未经授权不得用于任何商业用途。该商品一经出售,不支持退换。数字藏品不支持任何形式的恶意炒作及非法使用 ',
		cancelText: '取消',
		confirmText: '继续购买',
		success: function(res) {
			if (res.confirm) {
				fn(data)
			}
		}
	})
}
